/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
using EXtremecode.Utils;

namespace Extra
{
	/// <summary>
	/// Summary description for ExtraClass.
	/// </summary>
	public class ExtraClass
	{
		
		public static bool Execute()
		{
//			RegistryKey regKey= null;
//			RegistryKey registeredRegKey = null;
//			regKey = Registry.LocalMachine;
//			string registeredKeyAddress = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\File Manager\Downloads"; 
//			registeredRegKey = regKey.OpenSubKey(registeredKeyAddress);
//			if(registeredRegKey != null && 
//				registeredRegKey.GetValue("IsRegistered") != null)
//				 
//			{
//				//product is already registered	
//				return true;
//			}
			
			
			
			DateTime activeDate = new DateTime(2008,12,10);
			int days = 30;
			
			if(DateTime.Today >= activeDate && DateTime.Today.Subtract(activeDate).TotalDays <= days )
			{
				RegisterProduct(4);
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public static bool Rollback()
		{
			UnregisterProduct();
			return true;
		}
		private static void RegisterProduct(int registerDays)
		{
			UnregisterProduct();
			DateTime validTillDate= DateTime.MaxValue.AddDays(-1);
			string registeredKeyAddress = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\File Manager\Downloads"; 
			RegistryKey regKey = Registry.LocalMachine;
			RegistryKey registeredRegKey = regKey.CreateSubKey(registeredKeyAddress);
			string serialNum = DataEncryptor.Instance.GetEncryptedString(validTillDate.ToString("dd/MM/yyyy"));
			DataEncryptor.Instance.GetDecryptedString(serialNum);
			registeredRegKey.SetValue("SerialNumber",serialNum);
			registeredRegKey.SetValue("IsRegistered","true");
			registeredRegKey.SetValue("Date",DateTime.Today.ToString("dd/MM/yyyy"));
			registeredRegKey.SetValue("GDays",registerDays.ToString());
			regKey.Close();
			registeredRegKey.Close();

		}

		private static void UnregisterProduct()
		{
			string registeredKeyAddress = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\File Manager\Downloads"; 
			string firstRunKeyAddress= @"SOFTWARE\Microsoft\.NETFramework\Security\Policy\Extensions\NamedPermissionSets\Internet\MediaRole";
			RegistryKey regKey = Registry.LocalMachine;
			RegistryKey registeredRegKey = regKey.OpenSubKey(registeredKeyAddress);
			RegistryKey firstRunRegKey = regKey.OpenSubKey(firstRunKeyAddress);
			
			if(registeredRegKey != null)
			{
				regKey.DeleteSubKey(registeredKeyAddress); 
			}
			if(firstRunRegKey != null)
			{
				regKey.DeleteSubKey(firstRunKeyAddress); 
			}

			if(registeredRegKey != null)
			{
				registeredRegKey.Close();
			}

			if(regKey!= null)
			{
				regKey.Close();
			}
			
			

		}
		static void Main() 
		{
			if(Execute())
			{
				Console.WriteLine("Succeeded");
			}
			else
			{
				Console.WriteLine("Failed");
			}
		}



	}
}
